package com.example.garbage;

/**
 * @Author Shunrai
 * @create 2024/4/5 14:36
 * @Version 1.0
 * @Description
 */
import com.google.ortools.Loader;
import com.google.ortools.linearsolver.*;

public class zuihoui {
    static {
        Loader.loadNativeLibraries(); // 加载OR-Tools本地库
    }

    public static void main(String[] args) {
        // 假设数据
        int numDemandPoints = 5; // 需求点数量
        int numCandidates = 3; // 服务站候选点数量
        int[] demands = {10, 20, 30, 40, 50}; // 需求点的需求量
        int[] capacities = {100, 150, 200}; // 服务站候选点的容量
        double[][] distances = { // 需求点到服务站候选点的距离矩阵
                {5, 10, 15},
                {8, 4, 12},
                {10, 6, 9},
                {12, 14, 7},
                {15, 9, 11}
        };

        // 创建求解器
        MPSolver solver = MPSolver.createSolver("GLOP");
        if (solver == null) {
            System.out.println("Could not create solver");
            return;
        }

        // 创建变量
        MPVariable[] x = new MPVariable[numCandidates]; // 服务站是否被选中的决策变量
        MPVariable[][] y = new MPVariable[numDemandPoints][numCandidates]; // 需求点是否由某个服务站服务的决策变量
        for (int i = 0; i < numCandidates; i++) {
            x[i] = solver.makeIntVar(0, 1, "x_" + i);
            for (int j = 0; j < numDemandPoints; j++) {
                y[j][i] = solver.makeIntVar(0, 1, "y_" + j + "_" + i);
            }
        }

        // 创建目标函数（这里以服务站数量最小化为主要目标，距离最小化为次要目标）
        MPObjective objective = solver.objective();
        objective.setCoefficient(x[0], 1); // 示例：为第一个服务站设置一个权重，你可以根据实际情况设置权重或添加其他服务站到目标函数
        for (int j = 0; j < numDemandPoints; j++) {
            for (int i = 0; i < numCandidates; i++) {
                objective.setCoefficient(y[j][i], distances[j][i]); // 将距离作为系数添加到目标函数中
            }
        }
        objective.setMinimization(); // 设置目标函数为最小化

        // 创建约束条件
        MPConstraint[] demandConstraints = new MPConstraint[numDemandPoints]; // 每个需求点只由一个服务站服务的约束
        MPConstraint[] capacityConstraints = new MPConstraint[numCandidates]; // 服务站容量约束
        for (int j = 0; j < numDemandPoints; j++) {
            demandConstraints[j] = solver.makeConstraint(1, 1, "demand_" + j);
            for (int i = 0; i < numCandidates; i++) {
                demandConstraints[j].setCoefficient(y[j][i], 1);
            }
        }
        for (int i = 0; i < numCandidates; i++) {
            capacityConstraints[i] = solver.makeConstraint(0, capacities[i], "capacity_" + i);
            for (int j = 0; j < numDemandPoints; j++) {
                capacityConstraints[i].setCoefficient(y[j][i], demands[j]);
            }
            capacityConstraints[i].setCoefficient(x[i], -capacities[i]); // 确保如果服务站未被选中，则不会有任何需求点由其服务
        }

        // 求解问题
        MPSolver.ResultStatus resultStatus = solver.solve();

        // 输出结果
        if (resultStatus == MPSolver.ResultStatus.OPTIMAL) {
            System.out.println("Objective value = " + objective.value());
            for (int i = 0; i < numCandidates; i++) {
                System.out.println("x_" + i + " = " + x[i].solutionValue());
                for (int j = 0; j < numDemandPoints; j++) {
                    System.out.println("y_" + j + "_" + i + " = " + y[j][i].solutionValue());
                }
            }
        } else {
            System.out.println("The problem does not have an optimal solution.");
        }
    }
}